<?php
class ExpenditureTypesController extends AppController {
    var $name = 'ExpenditureTypes';

    function index() {
        $this->ExpenditureType->recursive = 0;
        $this->set('expenditureTypes', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid expenditure type', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('expenditureType', $this->ExpenditureType->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->ExpenditureType->create();
            $this->data['ExpenditureType']['end_date'] = null;
            if ($this->ExpenditureType->save($this->data)) {
                $this->Session->setFlash(__('The expenditure type has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The expenditure type could not be saved. Please, try again.', true));
            }
        }
        $expenditureTypeSources = $this->ExpenditureType->ExpenditureTypeSource->find('list',
                array('conditions' => array('ExpenditureTypeSource.lookup_type' => 'EXPENDITURE_TYPE_SOURCE')));
                
        $expenditureTypePeriodicities = array();
               
        $this->set(compact('expenditureTypeSources', 'expenditureTypePeriodicities'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid expenditure type', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->ExpenditureType->save($this->data)) {
                $this->Session->setFlash(__('The expenditure type has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The expenditure type could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->ExpenditureType->read(null, $id);
        }
        $expenditureTypeSources = $this->ExpenditureType->ExpenditureTypeSource->find('list',
                array('conditions' => array('ExpenditureTypeSource.lookup_type' => 'EXPENDITURE_TYPE_SOURCE')));
        $expenditureTypePeriodicities = $this->ExpenditureType->ExpenditureTypePeriodicity->find('list',
                array('conditions' => array('ExpenditureTypePeriodicity.lookup_type' => 'EXPENDITURE_TYPE_PERIODICITY')));
        $this->set(compact('expenditureTypeSources', 'expenditureTypePeriodicities'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for expenditure type', true));
            $this->redirect(array('action'=>'index'));
        }
        if (empty($this->data)) {
            $this->data = $this->ExpenditureType->read(null, $id);
        }
        if (!empty($this->data)) {
            // ACA SETEO EL UPDATED_BY
            $this->data['ExpenditureType']['end_date'] = date('Y-m-d H:i:s');
            if ($this->ExpenditureType->save($this->data)) {
                $this->Session->setFlash(__('Expenditure type deleted', true));
            } else {
                $this->Session->setFlash(__('Expenditure type was not deleted', true));
            }
        }
        $this->redirect(array('action' => 'index'));
    }
    
	function update_select(){

	    if(!empty($this->data['ExpenditureType']['expenditure_type_source'])) {              
	        $expenditureTypeSource = $this->data['ExpenditureType']['expenditure_type_source'];
	
	        //$expenditureType = $this->ExpenditureType->ExpenditureTypeSource->getCode($expenditureTypeSource);        
	        	        
	        if ($expenditureTypeSource == '6') // SPECIAL
	        {
	            $options =  array('empty' => ' ');
	        }                           
	        else
	        {                   
	            $options =  $this->ExpenditureType->ExpenditureTypePeriodicity->find('list',
	                    array('conditions' => array('ExpenditureTypePeriodicity.lookup_type' => 'EXPENDITURE_TYPE_PERIODICITY')));                              
	        }
	      }

	    if (!empty($this->data['ExpenditureType']['expenditure_type_periodicity'])){
	    	
	    	$expenditureTypePeriodicity = $this->data['ExpenditureType']['expenditure_type_periodicity'];
	    	
	    	if ($expenditureTypePeriodicity == 'PERIODIC')
	    	{
	    		//$this->render('../periodic_expenditure_types/add');
	    		$this->flash(__('new', true), array('action'=> '../periodic_expenditure_types/add')); 
	    	}
	    	else 
	    	{
	    		$this->render('add');
	    	}	    	
	    }
	    
	     

    	$this->set(compact('options'));       
	}
}
?>